<?php
/**
* queen文件
* ==============================================
* 版权所有 
* ----------------------------------------------
* 这不是一个自由软件，未经授权不许任何使用和传播。
* ==============================================
* @date: 2016-12-18
* @author: yqh
* @version:
*/
namespace Home\Controller;
use Think\Controller;
class CheckController extends Controller{
    /**
    * 函数用途描述: 登录页面
    * @date: 2016-12-21下午4:38:34
    * @author: yqh
    * @param:param
    * @return:
    */
	public function login() {
		$from = I("get.from");
		$this->assign('from',$from);
    	$this->display();
    }
	
    /**
    * 用户登录
    * @date: 2016-12-18 上午10:53:58
    * @author: yqh
    * @param: string $uname
    * @param：string $pwd
    * @return:
    */
    public function loginCheck() {
        $uname = I("post.uname");
        $pwd = I("post.pwd");
        $from = I("post.from");
        $error = "";
        if (empty($uname)) {
            $error = "用户名不能为空";
            $array = array('status' => 0,'error' => $error);
            $this->ajaxReturn($array);
        } else if (empty($pwd))  {
            $error = "密码不能为空";
            $array = array('status' => 0,'error' => $error);
            $this->ajaxReturn($array);
        }
        try {
        	if ($from == 'index') {
        		$table = 'user';
        	} else if ($from == 'admin') {
        		$table = 'admin_user';
        	}
            $user = M($table);
            $num = $user->where(array('uname' => $uname))->count();
            if ($num > 0) {
                $result = $user->where(array('uname' => $uname,'pwd' => md5($pwd)))->find();
                if ($result) {
	                if ($from == 'index') {
	        			$_SESSION['index_Info'] = $result;
	        		} else if ($from == 'admin') {
	        			$_SESSION['admin_Info'] = $result;
	        		}
                	$this->ajaxReturn(array('status' => 1));
                } else {
                	$error = "密码错误";
                	$array = array('status' => 0,'error' => $error);
                	$this->ajaxReturn($array);
                }
                
            } else {
                $error = "用户名不存在";
                $array = array('status' => 0,'error' => $error);
                $this->ajaxReturn($array);
            }
        } catch(\Exception $e) {
            throw $e;
        }
    }
    /**
    * 用户注册
    * @date: 2016-12-18 上午10:53:42
    * @author: yqh
    * @param:  string   $uname
    * @param    string  $pwd
    * @return:
    */
    public function registerCkeck() {
        $uname = I("post.uname");
        $pwd = I("post.pwd");
        $error = "";
        if (empty($uname)) {
            $error = "用户名不能为空";
            $array = array('status' => 0,'error' => $error);
            $this->ajaxReturn($array);
        } else if (empty($pwd))  {
            $error = "密码不能为空";
            $array = array('status' => 0,'error' => $error);
            $this->ajaxReturn($array);
        }
        try {
            $user = M("user");
            $num = $user->where(array('uname' => $uname))->count();
            if ($num > 0) {
                $error = "用户名已经存在";
                $array = array('status' => 0,'error' => $error);
                $this->ajaxReturn($array);
            } else {
                $data = array('uname' => $uname,
                            'pwd' => md5($pwd),
                            'updated_time' => date("Y-m-d",time()),
                            'created_time' => date("Y-m-d",time())
                );
                $result = $user->add($data);
                if ($result) {
                    $_SESSION['index_Info'] = $data;
                    $this->ajaxReturn(['status' => 1]);
                } else {
                    $error = "注册失败";
                    $array = array('status' => 0,'error' => $error);
                    $this->ajaxReturn($array);
                }
            }
        } catch(\Exception $e) {
            throw $e;
        }
    }
    
    /**
    * 函数用途描述: 注册页面
    * @date: 2016-12-21下午4:52:42
    * @author: yqh
    * @param:param
    * @return:
    */
    public function register() {
    	$this->display();
    }
    
    public function loginOut() {
    	$from = I("get.from");
    	if ($from == 'admin') {
    		$_SESSION['admin_Info'] = '';
    		header("Location:./index.php?s=/Home/admin");
    	} else {
    		$_SESSION['index_Info'] = '';
    		header("Location:./index.php?s=/Home/index");
    	}
    }
}